<template>
  <div class="car">
    <!-- 如果存在子卡信息 -->
    <CarItem v-if="isHasCarInfo" :cardInfo="cardInfo"></CarItem>
  </div>
</template>
<script>
import CarItem from "./carItem.vue";
export default {
  head() {
    return {
      title: "我的车队"
    };
  },
  components: {
    CarItem
  },
  data() {
    return {
      cardInfo: {}, //子卡信息
      isHasCarInfo: false //  是否存在子卡信息
    };
  },
  created() {
    this.getSoncardInfo();
  },
  methods: {
    // 获取子卡信息
    async getSoncardInfo() {
      // 173 的数据   userId： 481719
      // 174 的数据   userId： 484786
      const params = {
        enterpriseId: 174, //this.enterpriseId
        userId: 484786 //this.$store.state.authen.userInfo.userId,
      };
      const res = await this.$axios.$get(
        `/fleetCardSub/enterpriseId/${params.enterpriseId}/userId/${params.userId}`
      );
      if (!res.data) {
        console.log("没有子卡数据");
        // 再查询一下是否根据当前用户绑定了主卡
        const res2 = await this.getParentCardInfo();
        if (res2) {
          this.cardInfo = res2;
          this.isHasCarInfo = true;
          console.log(
            "当前用户绑定了主卡---111",
            res2,
            this.cardInfo,
            this.isHasCarInfo
          );
        } else {
          console.log("没有任何的子卡和主卡信息");
          this.isHasCarInfo = false;
          this.$router.push("/car/search");
        }
      } else {
        console.log("有子卡数据");
        this.isHasCarInfo = true;
        this.cardInfo = res.data;
      }
    },

    // 获取主卡信息
    async getParentCardInfo() {
      const params = {
        enterpriseId: 174, // this.enterpriseId
        keyword: "15022"
      };
      const res = await this.$axios.$get(
        `/fleetCard/enterpriseId/${params.enterpriseId}/keyword/${params.keyword}`
      );
      return res.data[0];
    }
  },
  beforeDestroy() {
    console.log("销毁");
  }
};
</script>
<style scoped lang="scss">
.car {
  width: 95vw;
  margin: auto;
}
</style>
